package com.easy.common.util;

import cn.hutool.crypto.digest.DigestUtil;

import javax.xml.parsers.SAXParser;

public class PasswordDecode {
    public static final int[] char_arr={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
    public static String encryption(String pass){
        //加密次数
        int index_count=(int)(Math.random()*char_arr.length);
        //加密盐值
        int index_salt=(int)(Math.random()+char_arr.length);
        String result=pass;
        for (int i=0;i<char_arr[index_count];i++){
            result=result+(char)char_arr[index_salt];
            result= DigestUtil.md5Hex(result);
        }
        result=(char)char_arr[index_count]+result+(char)char_arr[index_salt];
        return result;
    }
/**
 * 验证密码是否正确
 * @param pass 原文密码
 * @param decode 加密后密码
 * @return 如果正确返回true
 * */
    public static boolean decode(String pass,String decode){
        //加密次数
        char char_count=decode.charAt(0);
        //加密盐值
        char char_salt=decode.charAt(decode.length()-1);
        for (int i=0;i<char_count;i++){
            pass=pass+char_salt;
            pass=DigestUtil.md5Hex(pass);
        }
        return decode.equals(char_count+pass+char_salt);
    }
}
